Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
path-exists
Advanced tools
The path-exists npm package is used to check if a file or directory exists on the file system without using fs.existsSync. It is built on top of Node.js's fs.promises API and provides a simple promise-based interface.
Check if a path exists
This feature allows you to check if a file or directory exists asynchronously by returning a promise that resolves to either true or false.
const pathExists = require('path-exists');
(async () => {
const exists = await pathExists('/path/to/file');
console.log(exists);
//=> true or false
})();
Check if a path exists synchronously
This feature provides a synchronous way to check if a file or directory exists, returning a boolean value immediately.
const pathExists = require('path-exists');
const exists = pathExists.sync('/path/to/file');
console.log(exists);
//=> true or false
fs-extra is a package that extends the built-in fs module, providing additional methods and ensuring consistency across platforms. It includes the 'pathExists' and 'pathExistsSync' methods, which are similar to the functionality provided by path-exists. fs-extra offers a broader set of file system operations, making it a more comprehensive choice for file system interactions.
make-dir is a package that focuses on creating directories and their parent directories if they don't exist. While it doesn't provide a direct method to check for the existence of a path, it is related in the sense that it handles the existence of directories as part of its operation. It differs from path-exists as it is more about directory creation than existence checking.
Check if a path exists
Because fs.exists()
is being deprecated, but there's still a genuine use-case of being able to check if a path exists for other purposes than doing IO with it.
Never use this before handling a file though:
In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to
fs.exists()
andfs.open()
. Just open the file and handle the error when it's not there.
$ npm install --save path-exists
// foo.js
const pathExists = require('path-exists');
pathExists('foo.js').then(exists => {
console.log(exists);
//=> true
});
Returns a promise for a boolean of whether the path exists.
Returns a boolean of whether the path exists.
MIT © Sindre Sorhus
FAQs
Check if a path exists
We found that path-exists demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.